/**
 * Copyright (C) 2018-2021
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.distributor.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.enums.ShopCommonEnum;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.aop.ForbidSubmit;
import co.yixiang.modules.category.domain.YxStoreCategory;
import co.yixiang.modules.distributor.domain.DistributorPrice;
import co.yixiang.modules.distributor.domain.YxDistributor;
import co.yixiang.modules.distributor.service.DistributorPriceService;
import co.yixiang.modules.distributor.service.YxDistributorService;
import co.yixiang.modules.distributor.service.dto.YxDistributorQueryCriteria;
import co.yixiang.modules.product.domain.YxStoreProductAttrValue;
import co.yixiang.modules.product.service.YxStoreProductAttrValueService;
import co.yixiang.modules.product.service.dto.YxStoreProductAttrValueQueryCriteria;
import co.yixiang.modules.user.service.YxUserDistributorRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ysl
 * @date 2021-11-21
 */
@Api(tags = "商城:经销商管理")
@RestController
@RequestMapping("api")
public class DistributorController {

    private final YxDistributorService yxDistributorService;
    private final YxStoreProductAttrValueService yxStoreProductAttrValueService;
    private final DistributorPriceService distributorPriceService;
    private final YxUserDistributorRelationService yxUserDistributorRelationService;


    private final IGenerator generator;
    public DistributorController(YxDistributorService yxDistributorService, YxStoreProductAttrValueService yxStoreProductAttrValueService,
                                 DistributorPriceService distributorPriceService, YxUserDistributorRelationService yxUserDistributorRelationService, IGenerator generator) {
        this.yxDistributorService = yxDistributorService;
        this.yxStoreProductAttrValueService = yxStoreProductAttrValueService;
        this.distributorPriceService = distributorPriceService;
        this.yxUserDistributorRelationService = yxUserDistributorRelationService;
        this.generator = generator;
    }


    @Log("查询经销商")
    @ApiOperation(value = "查询经销商")
    @GetMapping(value = "/yxDistributor")
    //@PreAuthorize("hasAnyRole('admin','YXSTOREPRODUCT_ALL','YXSTOREPRODUCT_SELECT')")
    public ResponseEntity getDistributors(YxDistributorQueryCriteria criteria, Pageable pageable){

        Map<String, Object> queryAll = yxDistributorService.queryAll(criteria, pageable);

        return new ResponseEntity<>(queryAll,HttpStatus.OK);
    }

    @ApiOperation(value = "获取查询经销商信息")
    @GetMapping(value = "/yxDistributor/distributorInfo/")
    public ResponseEntity distributorInfo() {
        Map<String, Object> map = new LinkedHashMap<>(3);
        map.put("distributorInfoList",yxDistributorService.list());
        return new ResponseEntity<>(map,HttpStatus.OK);
    }
    @ApiOperation(value = "查询所有经销商")
    @GetMapping(value = "/distributorList")
    public ResponseEntity getDistributorList(){
        Map<String,Object> map = new LinkedHashMap<>(3);
        //经销商
        map.put("distributorList",yxDistributorService.list());
        return new ResponseEntity<>(map,HttpStatus.OK);
    }


    @ApiOperation(value = "新增")
    @PostMapping(value = "/addDistributor")
    //@PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YxDistributor resources){
        Date current = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        resources.setDistributorId(format.format(current) + Math.round(Math.random() * 2000));
        //查询所有的sku,分别插入distributorPrice
        /*List<YxStoreProductAttrValue> yxStoreProductAttrValues = yxStoreProductAttrValueService.queryAll(new YxStoreProductAttrValueQueryCriteria());
        List<DistributorPrice> distributorPrices = new ArrayList<>();
        for (YxStoreProductAttrValue storeProductAttrValue : yxStoreProductAttrValues) {
            DistributorPrice distributorPrice = new DistributorPrice();
            distributorPrice.setDistributorId(resources.getDistributorId());
            distributorPrice.setProductId(storeProductAttrValue.getProductId());
            distributorPrice.setProductSku(storeProductAttrValue.getId());
            distributorPrice.setProductSkuName(storeProductAttrValue.getSku());
            distributorPrices.add(distributorPrice);
        }
        boolean batchInsert = distributorPriceService.saveBatch(distributorPrices);*/
        return new ResponseEntity<>(yxDistributorService.save(resources),HttpStatus.CREATED);
        /*if (batchInsert) {
            return new ResponseEntity<>(yxDistributorService.save(resources),HttpStatus.CREATED);
        } else {
            return new ResponseEntity<>(null,HttpStatus.METHOD_FAILURE);
        }*/
    }


    @ApiOperation(value = "查询单条信息")
    @GetMapping(value = "/yxDistributor/info/{id}")
    //@PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_GET')")
    public ResponseEntity getInfo(@PathVariable Integer id){
        return new ResponseEntity<>(yxDistributorService.getById(id),HttpStatus.OK);
    }


    @ApiOperation(value = "修改")
    @PutMapping(value = "/editDistributor")
    public ResponseEntity updateDistributor(@Validated @RequestBody YxDistributor resources){
        yxDistributorService.saveOrUpdate(resources);
        //yxUserDistributorRelationService.refreshDistributor(yxDistributorService.getById(resources.getId()));
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }


    @ForbidSubmit
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/delDistributor/{id}")
    //@PreAuthorize("hasAnyRole('admin','YXARTICLE_ALL','YXARTICLE_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        yxDistributorService.removeById(id);
        //yxUserDistributorRelationService.refreshDistributor(yxDistributorService.getById(id));
        return new ResponseEntity(HttpStatus.OK);
    }


}
